package com.ats.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ats.model.Instructor;
import com.ats.model.Student;
import com.ats.model.Teacher;
import com.ats.model.User;
import com.ats.service.UserSerivce_xh;
import com.ats.serviceimpl.UserServiceImpl_xh;

import net.sf.json.JSONObject;
/**
 * 找回密码
 * @author xh
 *
 */
public class ForgetUpswController_xh extends HttpServlet{
	UserSerivce_xh us = new UserServiceImpl_xh();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter  pw = resp.getWriter();
		String newupsw =req.getParameter("newupsw");
		String uname = req.getParameter("uname");
		String jso = req.getParameter("jso");
		//获取用户权限
		int ustate =us.findUstateByUname(uname);
		//辅导员修改密码
		if(ustate==0){	
			JSONObject jsob=JSONObject.fromObject(jso);	
			Instructor ins =(Instructor)JSONObject.toBean(jsob, Instructor.class);
			int iid =us.findIidByUname(
					ins.getName(), ins.getAge(), ins.getAddress(), ins.getNumber(),uname);
			boolean flag =us.updateUpswByIid(uname, iid, newupsw);
			pw.write(flag+"");

		}
		//老师修改密码
		else if(ustate==1){			
			JSONObject jsob=JSONObject.fromObject(jso);	
			Teacher te =(Teacher)JSONObject.toBean(jsob, Teacher.class);
			int tid =us.findTidByUname(
					te.getName(), te.getAge(), te.getAddress(), te.getNumber(),uname);
			boolean flag =us.updateUpswByTid(uname, tid, newupsw);
			pw.write(flag+"");
		}
		//学生修改密码
		else if(ustate==2){
			JSONObject jsob=JSONObject.fromObject(jso);	
			Student stu =(Student)JSONObject.toBean(jsob, Student.class);
			int sid =us.findSidByUname(
					stu.getName(), stu.getAge(), stu.getAddress(), stu.getNumber(),uname);
			boolean flag =us.updateUpswBySid(uname, sid, newupsw);
			pw.write(flag+"");
		}
		pw.flush();
		pw.close();	

	}

}
